<?php
/**
 * User: Hufeng
 * Date: 2017/08/29 19:48
 * Desc: 用户Model
 */
namespace App\Model;
use SasPHP\SqlModel;

class UserExtModel extends SqlModel{
    protected $dbName = 'user_ext';

    public function __construct(){
        parent::__construct('DB_SERVER');
    }

    function insertData($data){
        $this->dbConn->insert($this->dbName,$data);
        return $this->dbConn->id();
    }

    function editData($where,$data){
        $this->dbConn->update($this->dbName,$data,$where);
    }

    function delData($data){
        $this->dbConn->delete($this->dbName, $data);
    }

    function findData($where,$fields='*'){
        $res = $this->dbConn->get($this->dbName,$fields,$where);
        return $res;
    }

    function queryData($where,$fields='*'){
        $res = $this->dbConn->select($this->dbName,$fields,$where);
        return $res;
    }
    //获取统计数据
    public function getTotalNum($where){
        $res = $this->dbConn->count($this->dbName,$where);
        return $res;
    }
    //sum数据
    public function sumData($where,$param){
        $res = $this->dbConn->sum($this->dbName,$param,$where);
        return $res;
    }

    //记录用户登录时间和IP
    public function loginData($uid,$ip,$time){
        $res = $this->findData(['uid'=>$uid,'ekey'=>'ip']);
        if($res){
            $this->editData(['uid'=>$uid,'ekey'=>'ip'],['eval'=>$ip]);
            $this->editData(['uid'=>$uid,'ekey'=>'loginTime'],['eval'=>$time]);
        }else{
            $this->insertData(['uid'=>$uid,'ekey'=>'ip','eval'=>$ip]);
            $this->insertData(['uid'=>$uid,'ekey'=>'loginTime','eval'=>$time]);
        }
        return;
    }
    //记录用户app版本信息
    public function appVerData($uid,$os,$ver){
        $res = $this->findData(['uid'=>$uid,'ekey'=>'os']);
        if($res){
            $this->editData(['uid'=>$uid,'ekey'=>'os'],['eval'=>$os]);
            $this->editData(['uid'=>$uid,'ekey'=>'appVer'],['eval'=>$ver]);
        }else{
            $this->insertData(['uid'=>$uid,'ekey'=>'os','eval'=>$os]);
            $this->insertData(['uid'=>$uid,'ekey'=>'appVer','eval'=>$ver]);
        }
        return;
    }
    //记录用户实名信息
    public function userRealData($uid,$cardAddress,$cardFront,$cardBack,$cardHand){
        $res = $this->findData(['uid'=>$uid,'ekey'=>'cardFront']);
        if($res){
            $this->editData(['uid'=>$uid,'ekey'=>'cardAddress'],['eval'=>$cardAddress]);
            $this->editData(['uid'=>$uid,'ekey'=>'cardFront'],['eval'=>$cardFront]);
            $this->editData(['uid'=>$uid,'ekey'=>'cardBack'],['eval'=>$cardBack]);
            $this->editData(['uid'=>$uid,'ekey'=>'cardHand'],['eval'=>$cardHand]);
        }else{
            $this->insertData(['uid'=>$uid,'ekey'=>'cardAddress','eval'=>$cardAddress]);
            $this->insertData(['uid'=>$uid,'ekey'=>'cardFront','eval'=>$cardFront]);
            $this->insertData(['uid'=>$uid,'ekey'=>'cardBack','eval'=>$cardBack]);
            $this->insertData(['uid'=>$uid,'ekey'=>'cardHand','eval'=>$cardHand]);
        }
        return;
    }
    //查询用户所有扩展信息
    public function userAllData($uid){
        $list = $this->queryData(['uid'=>$uid]);
        $data = [];
        foreach ($list as $val){
            $data[$val['ekey']] = $val['eval'];
        }
        return $data;
    }
}
